﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager
{
    public partial class frmAddCT : Form
    {
        SQL sql = new SQL();
        string strMNV;

        public frmAddCT()
        {
            InitializeComponent();
        }

        private void frmAddCT_Load(object sender, EventArgs e)
        {
            getValue();
        }

        public void getValue()
        {
            SqlParameter sqlPara = new SqlParameter("@MNV", strMNV);
            string strQuery = "sql_select_NhanVien";
            DataTable dt = sql.getDataTable(strQuery, sqlPara);
            if (dt == null)
            {
                MessageBox.Show("Please choose Nhan vien ?", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }

            tbMNV.Text = dt.Rows[0][0].ToString();
            cbMPB.Text = dt.Rows[0][1].ToString();
            tbnameNV.Text = dt.Rows[0][2].ToString();
            tbAddress.Text = dt.Rows[0][3].ToString();
            dateIntoCompany.Value = Convert.ToDateTime(dt.Rows[0][4].ToString());
            dateBirtday.Value = Convert.ToDateTime(dt.Rows[0][5].ToString());

            sqlPara = new SqlParameter("@MPB", cbMPB.Text);
            strQuery = "sql_MCT_where_MPB_cb";
            dt = sql.getDataTable(strQuery, sqlPara);
            if (dt == null)
            {
                MessageBox.Show("Please choose Nhan vien ?", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }
            cbMCT.DataSource = dt;
            cbMCT.ValueMember = "MCT";
            cbMCT.Text = "";
            tbNameCT.Clear();
        }

        private void cbMCT_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlParameter sqlPara = new SqlParameter("@MCT", cbMCT.Text);
            string strQuery = "sql_select_CT_where_MCT";
            try
            {
                DataTable dt = sql.getDataTable(strQuery, sqlPara);
                tbNameCT.Text = dt.Rows[0][2].ToString();
                dateCPCT.Value = Convert.ToDateTime(dt.Rows[0][3].ToString());
                dateKCCT.Value = Convert.ToDateTime(dt.Rows[0][4].ToString());
                dateHTCT.Value = Convert.ToDateTime(dt.Rows[0][5].ToString());
            }
            catch { }
        }

        public void getMNV(string strMNV)
        {
            this.strMNV = strMNV;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            SqlParameter[] sqlPara = new SqlParameter[2];
            sqlPara[0] = new SqlParameter("@MNV", strMNV);
            sqlPara[1] = new SqlParameter("@MCT", cbMCT.Text);
            string strQuery = "sql_insert_CT_NhanVien_CongTrinh";
            sql.execute_Query(strQuery, sqlPara);
        }
    }
}
